package LeetCode.LeetCode2;

public class Num22 {
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
    public ListNode getKthFromEnd(ListNode head, int k) {
        if(k == 0 && head == null){
            return head;
        }
        //双指针
        ListNode prev = head;
        ListNode cur = head;
        for (int i = 0; i < k; i++) {
            prev = prev.next;
        }
        while(prev != null){
            prev = prev.next;
            cur = cur.next;
        }
        return cur;
    }
}
